﻿using System;
using System.Data;
using System.Data.SqlClient;

namespace HRMService
{
    public partial class MainService : ICLanguage
    {
        public DataSet CLanguage_GetList(out int ItemCount, int CandidateID, int currentPage, int pageSize)
        {
            DataSet retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_CandidateLanguage_GetList", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@CandidateID", CandidateID);
            dbCmd.Parameters.AddWithValue("@CurrentPage", currentPage);
            dbCmd.Parameters.AddWithValue("@PageSize", pageSize);
            try
            {
                retVal = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);
                ItemCount = Convert.ToInt32(retVal.Tables[1].Rows[0][0]);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public CLanguageInfo CLanguage_GetInfo(int _CLanguage_ID)
        {
            CLanguageInfo retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_CandidateLanguage_GetInfo", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@CLanguage_ID", _CLanguage_ID);
            SqlDataReader dr = null;

            try
            {
                dbConn.Open();
                dr = dbCmd.ExecuteReader();
                if (dr.Read())
                {
                    retVal = new CLanguageInfo();
                    retVal.ID = Convert.ToInt32(dr["CLanguage_ID"]);
                    retVal.CandidateID = Convert.ToInt32(dr["CLanguage_CandidateID"]);
                    retVal.Name = Convert.ToString(dr["CLanguage_Name"]);
                    retVal.SelfRating = Convert.ToString(dr["CLanguage_SelfRating"]);
                    retVal.CommentsOfInterview = Convert.ToString(dr["CLanguage_CommentsOfInterview"]);
                }
            }
            finally
            {
                if (dr != null) dr.Close();
                dbConn.Close();
            }
            return retVal;
        }

        public int CLanguage_Insert(CLanguageInfo eInfo)
        {
            int retVal = 0;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_CandidateLanguage_Insert", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;

            dbCmd.Parameters.AddWithValue("@CLanguage_CandidateID", eInfo.CandidateID);
            dbCmd.Parameters.AddWithValue("@CLanguage_Name", eInfo.Name);
            dbCmd.Parameters.AddWithValue("@CLanguage_SelfRating", eInfo.SelfRating);
            dbCmd.Parameters.AddWithValue("@CLanguage_CommentsOfInterview", eInfo.CommentsOfInterview);


            try
            {
                dbConn.Open();
                retVal = Convert.ToInt32(dbCmd.ExecuteScalar());
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public bool CLanguage_Update(CLanguageInfo eInfo)
        {
            bool retVal = false;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_CandidateLanguage_Update", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@CLanguage_ID", eInfo.ID);
            dbCmd.Parameters.AddWithValue("@CLanguage_CandidateID", eInfo.CandidateID);
            dbCmd.Parameters.AddWithValue("@CLanguage_Name", eInfo.Name);
            dbCmd.Parameters.AddWithValue("@CLanguage_SelfRating", eInfo.SelfRating);
            dbCmd.Parameters.AddWithValue("@CLanguage_CommentsOfInterview", eInfo.CommentsOfInterview);
            try
            {
                dbConn.Open();
                if (dbCmd.ExecuteNonQuery() > 0)
                {
                    retVal = true;
                }
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public bool CLanguage_Delete(int CLanguageID)
        {
            bool retVal = false;
            string cmdText = "delete from hrm_candidatelanguage where CLanguage_ID = @CLanguage_ID";
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand(cmdText, dbConn);
            dbCmd.CommandType = CommandType.Text;
            dbCmd.Parameters.AddWithValue("@CLanguage_ID", CLanguageID);

            try
            {
                dbConn.Open();
                if (dbCmd.ExecuteNonQuery() > 0)
                {
                    retVal = true;
                }
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public bool CLanguage_Exist(int _ID, int _CandidateID, string _No)
        {
            bool retVal = false;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_CandidateLanguage_Exist", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@CLanguage_ID", _ID);
            dbCmd.Parameters.AddWithValue("@CLanguage_CandidateID", _CandidateID);
            dbCmd.Parameters.AddWithValue("@CLanguage_Name", _No);
            try
            {
                dbConn.Open();
                if (Convert.ToInt32(dbCmd.ExecuteScalar()) > 0)

                    retVal = true;
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
    }
}
